
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/**
 * 普通输入文本
 */
class CommFormInputWidget extends StatefulWidget {
  String? title;
  TextStyle? titleStyle;
  EdgeInsetsGeometry? titleMargin;

  Function? onSubmitted;
  Function? onChanged;
  List<TextInputFormatter>?inputFormatters;
  String? defaultCont;
  double? width;
  double? height;
  bool? readOnly;
  TextInputType? keyboardType;
  TextInputAction? textInputAction;
  EdgeInsetsGeometry? contentPadding;
  String? hintText;
  Color? borderColor;
  double? borderRadius;
  BoxDecoration? decoration;
  int? maxLength;
  int? maxLines;
  int? minLines;
  bool? isNecessary;
  Alignment? outAlignment;
  TextAlign? inputTextAlign;
  double? hintFontSize;
  double? fontSize;
  Color? hintFontColor;
  Color? fontColor;
  CommFormInputWidget({
    Key? key,
    this.title,
    this.outAlignment,this.inputTextAlign,
    this.titleStyle,this.titleMargin,
    this.onSubmitted,this.onChanged,this.inputFormatters,this.defaultCont,this.width,this.height,
    this.readOnly,this.keyboardType,this.textInputAction,this.contentPadding,this.hintText,this.borderColor,
    this.borderRadius,this.decoration,this.maxLength,this.maxLines,this.minLines,this.isNecessary,
    this.hintFontSize,this.hintFontColor,this.fontSize,this.fontColor,
  }):super(key: key);
  @override
  _CommFormInputWidgetState createState() => _CommFormInputWidgetState();
}

class _CommFormInputWidgetState extends State<CommFormInputWidget> {
  late TextEditingController textEditingController;
  @override
  void initState() {
    // TODO: implement initState
    textEditingController=TextEditingController(text: widget.defaultCont);
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    if(textEditingController!=null)textEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        (widget.title??'').isEmpty?Container():Container(
          child: Row(
            children: [
              (widget.isNecessary??false)?Text(
                '*',
                style: widget.titleStyle??TextStyle(
                    fontSize: px_14,
                    color: color_red
                ),
              ):Container(),
              Expanded(child: Text(
                '${widget.title}',
                style: widget.titleStyle??TextStyle(
                    fontSize: px_14,
                    color: color_777777
                ),
              )),

            ],
          ),
          margin: widget.titleMargin??EdgeInsets.only(
            bottom: px_8.h
          ),
        ),
        Container(
          width: widget.width??MediaQuery.sizeOf(context).width,
          height:widget.height??px_78,
          margin: EdgeInsets.all(px_0),
          decoration: widget.decoration??BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(widget.borderRadius??px_4)),
              border: Border.all(
                  color: widget.borderColor??color_d4d4d4
              ),
          ),
          alignment: widget.outAlignment??Alignment.topLeft,
          child: TextFormField(
            style: TextStyle(
                fontSize: widget.fontSize??px_14,
                color: widget.fontColor??color_383838
            ),
            enabled:true ,
            textAlign: widget.inputTextAlign??TextAlign.start,
            maxLength: widget.maxLength??100,
            maxLines: widget.maxLines??3,
            minLines: widget.minLines??3,
            obscureText: false,
            autofocus: false,
            controller: textEditingController,
            readOnly: widget.readOnly??false,
            textInputAction: widget.textInputAction??TextInputAction.done,
            keyboardType: widget.keyboardType??TextInputType.text,
            onFieldSubmitted: (val){
              if(widget.onSubmitted!=null)widget.onSubmitted!(val);
            },
            onChanged: (value) {
              if(widget.onChanged!=null)widget.onChanged!(value);
            },
            cursorColor: color_B3895D,
//          // 数字、手机号限制格式为0到9(白名单)， 密码限制不包含汉字（黑名单）
            inputFormatters:widget.inputFormatters,
//                          inputFormatters: (keyboardType == TextInputType.number || keyboardType == TextInputType.phone) ?
//                          [WhitelistingTextInputFormatter(RegExp("[0-9]"))] : [BlacklistingTextInputFormatter(RegExp("[\u4e00-\u9fa5]"))],
            decoration: InputDecoration(
                contentPadding: widget.contentPadding??EdgeInsets.only(
                  left: px_5,
                  right: px_5,
                  top: px_2,
                  bottom: px_2,
                ),
                isDense: true,
                hintText: widget.hintText??'${string_please_input}',
                hintStyle: TextStyle(
                    fontSize: widget.hintFontSize??px_14,
                    color: widget.hintFontColor??color_999999
                ),
                counterText: "",
                focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                        color: color_transparent,
                        width: 1
                    )
                ),
                enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                        color: color_transparent,
                        width: 1
                    )
                )
            ),
          ),
        )
      ],
    );
  }
}
